<template>
  <view class="content">
    <!-- 库存查询 -->
    <blueTeethBtn></blueTeethBtn>
    <u-form
      ref="uForm"
      label-width="150">
      <view class="oneCard">
        <view class="line-view">
          <uni-data-select
            v-model="factoryId"
            :localdata="factorieList"
            placeholder="选择工厂"></uni-data-select>
        </view>
        <view class="line-view">
          <u-input
            v-model="scanSearchText"
            border
            focus
            placeholder="扫描仓位"
            :placeholderStyle="placeholderStyle"
            @confirm="checkInfo"
            @focus="scanSearchText = ''" />
        </view>
        <view class="line-view">
          <u-input
            v-model="barcode"
            border
            placeholder="扫描条码"
            :placeholderStyle="placeholderStyle"
            @confirm="checkInfo"
            @focus="barcode = ''" />
        </view>
      </view>
      <view
        v-for="(item, index) in formModel"
        :key="index"
        class="oneCard"
        style="padding-bottom: 10rpx; margin-top: 10rpx">
        <!-- <view class="print-view">
          <view
            class="print-btn"
            @click="doPrint(item)">
            打印
          </view>
        </view> -->
        <u-form-item label="物料代码:">{{ item.productCode }}</u-form-item>
        <u-form-item label="物料名称:">{{ item.productName }}</u-form-item>
        <u-form-item label="规格型号:">{{ item.productModel }}</u-form-item>
        <!-- 同种物料不同批次，下面循环 -->
        <view
          v-for="(itemLit, indexLit) in item.inventoryList"
          :key="indexLit"
          class="oneLit"
          style="margin-bottom: 30rpx; border-left: 2rpx solid #cdcdcd">
          <u-form-item label-width="160">
            <view
              class="print-btn"
              @click="doPrint(itemLit)">
              打印
            </view>
          </u-form-item>
          <u-form-item
            label-width="160"
            label="条码:">
            {{ itemLit.barcode }}
          </u-form-item>
          <u-form-item
            label-width="160"
            label="批号:">
            {{ itemLit.batch }}
          </u-form-item>
          <u-form-item
            label-width="160"
            label="数量:">
            {{ itemLit.availableQuality }}
          </u-form-item>
          <u-form-item
            label-width="160"
            label="入库日期:">
            {{ itemLit.createTime }}
          </u-form-item>
          <u-form-item
            label-width="160"
            label="生产日期:">
            {{ itemLit.productTime }}
          </u-form-item>
          <u-form-item
            label-width="160"
            label="计划跟踪号:">
            {{ itemLit.fmtono }}
          </u-form-item>
          <u-form-item
            label-width="160"
            label="仓库:">
            {{ itemLit.stockName }}
          </u-form-item>
          <u-form-item
            label-width="160"
            label="仓位:">
            {{ itemLit.stockPositionName }}
          </u-form-item>
        </view>
        <!-- 同种物料不同批次，下面循环 -->
      </view>
    </u-form>
    <div class="pagination-view">
      <uni-pagination
        :current="page"
        :total="totalElements"
        :show-icon="false"
        @change="pageChange"
        :pageSize="pageSize"
        style="width: 720rpx" />
    </div>
    <u-toast ref="uToast" />
    <u-back-top :scroll-top="scrollTop"></u-back-top>
  </view>
</template>

<script>
var that;
import * as bter from '@/components/printCom/bter.js';
import * as zksdk from '@/components/printCom/bluetoolth.js';
import blueTeethBtn from '@/components/blueTeethBtn/blueTeethBtn.vue';
export default {
  components: {
    blueTeethBtn,
  },
  data() {
    return {
      page: 1,
      pageSize: 10,
      totalElements: 0,
      factoryId: null,
      scanSearchText: '',
      placeholderStyle: 'font-size:25rpx',
      formModel: [],
      factorieList: [],
      barcode: null,
      scrollTop: 0,
    };
  },
  onLoad() {
    that = this;
    that.getList();
  },
  onPageScroll(e) {
    this.scrollTop = e.scrollTop;
  },
  methods: {
    doPrint(e) {
      console.log(e, 'czq');
      uni.showModal({
        title: '提示',
        content: '是否打印此条数据？',
        success: function (res) {
          if (res.confirm) {
            that.writeBLECharacteristicValue(e);
          }
        },
      });
    },

    // 发送二进制数据
    writeBLECharacteristicValue(obj) {
      console.log(obj);
      // obj = {
      //   stackCode: '202310080002', //堆码
      //   projectNumber: 'YD230928', //项目号
      //   materialCode: 'SEW007812112', // 图号
      //   materialName: '立柱焊接T909L.2.11.2', //物料名称
      // };
      let deviceId = getApp().globalData.deviceId;
      if (!deviceId) {
        uni.showToast({
          icon: 'none',
          title: '缺少设备Id,请去蓝牙页面设置',
        });
        return;
      }
      let strCmd = zksdk.CreatCPCLPage(576, 400, 1, 0);
      strCmd += zksdk.addCPCLQRCode(340, 20, 'Q', 2, 7, obj.barcode);
      strCmd += zksdk.addCPCLText(80, 20, '7', '2', 0, obj.barcode);
      strCmd += zksdk.addCPCLText(10, 80, '7', '2', 0, '编号:');
      strCmd += zksdk.addCPCLText(100, 80, '7', '2', 0, obj.productCode);
      strCmd += zksdk.addCPCLText(10, 150, '7', '2', 0, '单位:');
      strCmd += zksdk.addCPCLText(100, 150, '7', '2', 0, obj.productUnitName);
      strCmd += zksdk.addCPCLText(10, 220, '7', '2', 0, '名称:');
      strCmd += zksdk.addCPCLText(100, 220, '7', '2', 0, obj.productName);
      strCmd += zksdk.addCPCLText(10, 290, '7', '2', 0, '规格:');
      strCmd += zksdk.addCPCLText(100, 290, '7', '2', 0, obj.productModel);
      strCmd += zksdk.addCPCLGAP();
      strCmd += zksdk.addCPCLPrint();
      bter.BTER.printSomething(strCmd);
    },
    pageChange(e) {
      uni.showLoading({
        mask: true,
        title: '加载中...',
      });
      that.page = e.current;
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 0,
      });
      that.confirmOne();
    },
    getList() {
      that.$u.post('/wms/mobile/api/getFactories', {}).then((res) => {
        const list = res.data;
        for (const item of list) {
          item.text = item.name;
          item.value = item.id;
        }
        that.factorieList = list;
        this.factoryId = that.factorieList[0].id;
      });
    },
    checkInfo() {
      that.page = 1;
      that.confirmOne();
    },
    confirmOne() {
      if (!that.factoryId) {
        that.$refs.uToast.show({
          title: '请先选择工厂',
          type: 'error',
        });
        return false;
      }
			uni.showLoading({
				mask:true,
				title:'请稍后...'
			})
      // 根据仓位code查询库存
      // if (!this.scanSearchText) {
      // 	return;
      // }
      // this.formModel = [];
      this.$u
        .get('/wms/mobile/api/getDetailInventorys', {
          stockPositionCode: this.scanSearchText,
          factoryId: that.factoryId,
          barcode: that.barcode,
          page: that.page - 1,
          size: that.pageSize,
        })
        .then((res) => {
          if (res.code != 0) {
            that.$refs.uToast.show({
              title: res.message,
              type: 'error',
            });
            return;
          }
          this.formModel = res.data.content;
          that.totalElements = res.data.totalElements;
        });
    },
    backPage() {
      uni.navigateBack({
        delta: 1,
      });
    },
  },
};
</script>

<style lang="scss">
page {
  background-color: #f8f8f8;
}

.content {
  height: 100%;
}

.oneCard {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 10rpx 25rpx;
}

/deep/.uni-easyinput__content-input {
  font-size: 32rpx;
}

.btnRow {
  text-align: center;
}

.u-form-item {
  padding: 0 0;
}

.oneLit {
  padding: 0 0 0 20rpx;
}

.print-view {
  width: 700rpx;
  padding: 15rpx 0 0 0;


}

  .print-btn {
    width: 105rpx;
    height: 55rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 30rpx;
    border-radius: 10rpx;
    background: #3069ee;
		margin: 10rpx 0;
		margin-left: auto;
  }
</style>
